﻿
using SqlSugar.Extensions;

namespace TestPressure
{
    public partial class AddProjectForm: BaseForm
    {
        #region 属性

        private Project obj = new Project();

        #endregion

        #region 自定义方法

        /// <summary>重复检测
        /// 
        /// </summary>
        /// <returns></returns>
        private bool CheckRepeat()
        {
            Dictionary<string, Object> where = new Dictionary<string, Object>();
            where["project_number"] = ProjectNumbernput.Text;
            List<Project> list = SqlSugarHelper.Inquireable<Project>(where);
            int count = list.Count;
            if (count > 0)
            {
                Error(where["project_number"] + "工程已存在");
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>格式化判断方式
        /// 
        /// </summary>
        /// <param name="estimate"></param>
        /// <returns></returns>
        private int FormatEstimate(string str)
        {
            if (str == "固定数值")
            {
                return 0;
            }
            else
            {
                return 1;
            }
        }

        #endregion

        #region 系统方法
        public AddProjectForm()
        {
            InitializeComponent();

        }

        private void AddTemplateForm_Load(object sender, EventArgs e)
        {
            this.Header.Text = "添加模板";
            Size size = new Size(int.Parse((467 * this.Dpi()).ToString()), int.Parse((416 * this.Dpi()).ToString()));
            this.MinimumSize = size;
            this.MaximumSize = size;
        }
        #endregion

        #region 点击事件
        private void ConfirmButton_Click(object sender, EventArgs e)
        {
            if (ProjectNumbernput.Text.Length == 0)
            {
                Alert.Error("请输入工程编号", this);
                ProjectNumbernput.Focus();
                return;
            }
            if (ProjectNameInput.Text.Length == 0)
            {
                Alert.Error("请输入工程名称", this);
                ProjectNameInput.Focus();
                return;
            }
            if (PipeLengthInput.Text.Length == 0)
            {
                Alert.Error("请输入管道长度", this);
                PipeLengthInput.Focus();
                return;
            }
            /*
            if (templateObj.template_number <= 0)
            {
                Alert.Error("请选择试压模板", this);
                return;
            }*/

            bool repeatRes = CheckRepeat();
            if (repeatRes)
            {
                return;
            }

            obj.project_number = ProjectNumbernput.Text;
            obj.project_name = ProjectNameInput.Text;
            obj.stake_mark = StakeMarkInput.Text;
            obj.pipe_specification = PipeSpecificationInput.Text;
            obj.pipe_length = float.Parse(PipeLengthInput.Text);
            obj.medium = MediumInput.Text;
            obj.design_pressure = float.Parse(DesignPressureInput.Text);
            obj.pipe_nature = PipeNatureSelect.Text;
            obj.test_type = TestTypeSelect.Text;
            obj.company = CompanyInput.Text;
            obj.manometer_precision = ManometerPrecisionSelect.Text;
            obj.manometer_range = ManometerRangeSelect.Text;
            obj.template_id = 0;

            List<Project> list = new List<Project>();
            list.Add(obj);
            int res = SqlSugarHelper.Insertable(list);
            if (res > 0)
            {
                if (this.Owner is not null)
                {
                    ProjectManageForm frm = (ProjectManageForm)Owner;
                    frm.LoadList();
                }
                this.Close();
            }
            else
            {
                Alert.Error("添加模板失败", this);
            }
        }
        #endregion
    }
}
